#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: tap_connector01_mysql.py
@version:
@time: 2022/01/13 
"""
import pymysql

from TapClientConnector.connector.tap_connector00_base import *


class TAPMysqlConnector(TAPConnectorBase):

    def __init__(self, kwargs):
        super().__init__(kwargs)
        self.connector = pymysql.connect(**self.FORMAT_DB_INFO)

    @connector_query_logger
    def show_dbs(self):
        query_str = "SHOW DATABASES"
        table_list = []
        for i in self._query(query_str):
            table_list.append(i[0])
        return table_list

    @connector_query_logger
    def show_tables(self):
        query_str = "SHOW TABLES"
        db_list = []
        for i in self._query(query_str):
            db_list.append(i[0])
        return db_list

    # ***** Generate *****

    # ** DDL **

    @generate_logger
    def generate_create(self, table_column, table=None):
        """
        通过表名，字段 类型生成建表语句
        :param table:
        :param table_column:
        :return:
        """
        if table:
            self.CURRENT_TABLE = table
        return f"""
                CREATE TABLE 
               {self.connector_database}.{self.CURRENT_TABLE} 
               {table_column}"""

    @generate_logger
    def generate_drop(self, table=None):
        if table:
            self.CURRENT_TABLE = table
        return f'DROP TABLE {self.connector_database}.{self.CURRENT_TABLE}'

    # ** DML **

    @generate_logger
    def generate_insert(self, insert, table=None):
        """
        :param table:  table
        :param insert: [columns, values] | [values]
        :return:
        """
        if table:
            self.CURRENT_TABLE = table
        if len(insert) == 1:
            return f"""
                    INSERT INTO
                    {self.connector_database}.{self.CURRENT_TABLE}
                    VALUES ({insert[0]})"""
        if len(insert) == 2:
            return f"""
                    INSERT INTO
                    {self.connector_database}.{self.CURRENT_TABLE} ({insert[0]})
                    VALUES ({insert[1]})"""

    @generate_logger
    def generate_update(self, update, where, table=None):
        """
        :param table:
        :param update: {column: value}
        :param where: WHERE ...
        :return:
        """
        if table:
            self.CURRENT_TABLE = table
        _update_list = [f'{k}={v}' for k, v in update.items()]
        COLUMNS = ",".join(_update_list)
        return f"""
               UPDATE 
               {self.connector_database}.{self.CURRENT_TABLE}
               SET {COLUMNS} {where}"""

    @generate_logger
    def generate_delete(self, where, table=None):
        """
        :param table:
        :param where:
        :return:
        """
        if table:
            self.CURRENT_TABLE = table
        return f"""
                DELETE FROM
                {self.connector_database}.{self.CURRENT_TABLE} {where}
                """

    @generate_logger
    def generate_select(self, column='*', where='', table=None, order=''):
        if table:
            self.CURRENT_TABLE = table
        return f"""
                SELECT {column} FROM
                {self.connector_database}.{self.CURRENT_TABLE} {where} {order}
                """

    @generate_logger
    def generate_alter_add(self, table, column, _type, other=''):
        if table:
            self.CURRENT_TABLE = table
        return f"""
                ALTER TABLE {table} ADD {column} {_type} {other}
                """

    @generate_logger
    def generate_alter_modify(self, table, column, _type):
        if table:
            self.CURRENT_TABLE = table
        return f"""
                ALTER TABLE {table} MODIFY {column} {_type}
                """

    @generate_logger
    def generate_alter_change(self, table, old_column, new_column, _type):
        if table:
            self.CURRENT_TABLE = table
        return f"""
                ALTER TABLE {table} CHANGE {old_column} {new_column} {_type}
                """

    @generate_logger
    def generate_alter_drop(self, table, column):
        if table:
            self.CURRENT_TABLE = table
        return f"""
                ALTER TABLE {table} DROP {column}
                """


if __name__ == '__main__':
    T = TAPMysqlConnector(Mysql.docker_mysql_8023_target)
    print(T.query('select * from B_GAUSS1648443847598251541'))
    print(T.TABLE_DESCRIPTION)
